home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1999 August / Macworld (1999-08).dmg / Shareware World / Info / For Developers / MADE 1.4.0 / Essentials / Essential Errors.c next >
Text File  |  1999-05-26  |  6KB  |  241 lines

  1. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  2. /*                                                                   */
  3. /*        MADE - Macintosh Application Development Essentials        */
  4. /*        ---------------------------------------------------        */
  5. /*           (c) Sig Software, http://www.sigsoftware.com/           */
  6. /*                                                                   */
  7. /* These files can only be used for experimental purposes. To obtain */
  8. /* fully commented code, source code for the functions in Essential  */
  9. /*   Extras.h and permission for usage in final projects, you must   */
  10. /*    purchase a license. See documentation for more information.    */
  11. /*                                                                   */
  12. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  13. /*                                                                   */
  14. /*  Essential Errors.c                                               */
  15. /*  ------------------                                               */
  16. /*                                                                   */
  17. /*  Error checking, handling, reporting and assertion support.       */
  18. /*                                                                   */
  19. /*  Version 1.0.0 - 10th November 1996                               */
  20. /*  Version 1.1.0 - 29th January 1998 - Updated for new Mac Header   */
  21. /*  Version 1.2.0 - 20th November 1998 - Avoids C/C++ warnings       */
  22. /*  Version 1.4.0 - 26th May 1999 - Contexts, hiding, new alert      */
  23. /*                                                                   */
  24. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  25.  
  26. #include "Essential Headers.h"
  27. #include "Essential Prototypes.h"
  28. #include <Sound.h>
  29.  
  30. typedef struct HidingLink HidingLink;
  31.  
  32. struct HidingLink {
  33.     HidingLink        *next;
  34.     Error            *keepError;
  35. };
  36.  
  37. typedef struct ContextLink ContextLink;
  38.  
  39. struct ContextLink {
  40.     ContextLink        *next;
  41.     Str255            contextString;
  42. };
  43.  
  44. HidingLink            *firstHiding=0;
  45. ContextLink            *firstContext=0;
  46.  
  47. Boolean HideErrors(Error* keepError)
  48. {
  49.     Error            error;
  50.     HidingLink        *link;
  51.  
  52.     link=(HidingLink*)AllocPtr(&error, sizeof(HidingLink));
  53.         _i(error)
  54.     link->keepError=keepError;
  55.     
  56.     AddToLinkedList(&firstHiding, link, firstHiding);
  57.     
  58.     return true;
  59.     _e
  60.     return false;
  61. }
  62.  
  63. void ShowErrors()
  64. {
  65.     HidingLink        *link;
  66.     
  67.     link=firstHiding;
  68.     Assert(link);
  69.     RemoveFromLinkedList(&firstHiding, link);
  70.     DestroyPtr(link);
  71. }
  72.  
  73. Boolean AddErrorContext(Str255 contextString)
  74. {
  75.     Error        error;
  76.     ContextLink    *link;
  77.     
  78.     link=(ContextLink*)AllocPtr(&error, sizeof(ContextLink));
  79.         _i(error)
  80.     BlockMove(contextString, link->contextString, 1+*contextString);
  81.     
  82.     AddToLinkedList(&firstContext, link, firstContext);
  83.     
  84.     return true;
  85.     _e
  86.     return false;
  87. }
  88.  
  89. void RemoveErrorContext()
  90. {
  91.     ContextLink        *link;
  92.     
  93.     link=firstContext;
  94.     Assert(link);
  95.     RemoveFromLinkedList(&firstContext, link);
  96.     DestroyPtr(link);
  97. }
  98.  
  99. void TestError(Error error)
  100. {
  101.     Str255            contextString, IDString, errorString, solutionString;
  102.     short            numString, totalStrings, compareError;
  103.     Handle            errorsResource;
  104.     unsigned char    *errorStrings, appendLen;
  105.     HidingLink        *hiding;
  106.     ContextLink        *context;
  107.     
  108.     if (error) {
  109.     
  110.         if (firstHiding) {
  111.             hiding=firstHiding;
  112.             while (hiding) {
  113.                 *(hiding->keepError)=error;
  114.                 hiding=hiding->next;
  115.             }
  116.             
  117.             SysBeep(1);
  118.             
  119.         } else {
  120.             NumToString(error, IDString);
  121.             
  122.             errorsResource=GetResource('Err2', 128);
  123.             if (ResError() || errorsResource==0)
  124.                 SysBeep(1);
  125.             
  126.             else {
  127.                 errorStrings=(unsigned char*)*errorsResource;
  128.                 totalStrings=*((short*)errorStrings)++;
  129.                 
  130.                 for (numString=0; numString<totalStrings; numString++) {
  131.                     compareError=*((short*)errorStrings)++;
  132.                     
  133.                     if (compareError==error)
  134.                         BlockMove(errorStrings, errorString, *errorStrings+1);
  135.                     
  136.                     errorStrings+=*errorStrings+1;
  137.                     
  138.                     if (compareError==error) {
  139.                         BlockMove(errorStrings, solutionString, *errorStrings+1);
  140.                         goto stringFound;
  141.                     }
  142.                         
  143.                     errorStrings+=*errorStrings+1;
  144.                 }
  145.  
  146.                 *errorString=0;
  147.                 *solutionString=0;
  148.                 
  149.                 stringFound:
  150.                 if (*errorString==0)
  151.                     GetIndString(errorString, 32765, 1);
  152.                 if (*solutionString==0)
  153.                     GetIndString(solutionString, 32765, 1);
  154.                 
  155.                 *contextString=0;
  156.                 context=firstContext;
  157.                 while (context) {
  158.                     appendLen=*(context->contextString);
  159.                     if (((int)*contextString+(int)appendLen)>255)
  160.                         appendLen=(unsigned char)(255-*contextString);
  161.                     
  162.                     BlockMove(context->contextString+1, contextString+*contextString+1, appendLen);
  163.                     *contextString+=appendLen;
  164.                     
  165.                     context=context->next;
  166.                 }
  167.                 
  168.                 ParamText(contextString, IDString, errorString, solutionString);
  169.                 SetCursor(&qd.arrow);
  170.                 Alert(32766, 0);
  171.             }
  172.         }
  173.     }
  174. }
  175.  
  176. Error TestResError(void* resource)
  177. {
  178.     Error    error;
  179.     
  180.     error=ResError();
  181.     if (!error)
  182.         if (resource==0L)
  183.             error=resNotFound;
  184.     TestError(error);
  185.     
  186.     return error;
  187. }
  188.  
  189. Error TestMemError(void* pointer)
  190. {
  191.     Error    error;
  192.     
  193.     error=MemError();
  194.     if (!error)
  195.         if (pointer==0L)
  196.             error=memFullErr;
  197.     TestError(error);
  198.     
  199.     return error;
  200. }
  201.  
  202. #if Project_Under_Development
  203.  
  204. void HandleAssertFailure(char* file, char* date, int line)
  205. {
  206.     Str255            fileString, dateString, lineString, memoryString;
  207.     unsigned char    *stringPtr;
  208.     
  209.     stringPtr=fileString+1;
  210.     while (*file)
  211.         *stringPtr++=(unsigned char)*file++;
  212.     *fileString=(unsigned char)(stringPtr-fileString-2);
  213.     
  214.     stringPtr=dateString+1;
  215.     while (*date)
  216.         *stringPtr++=(unsigned char)*date++;
  217.     *dateString=(unsigned char)(stringPtr-dateString-2);
  218.     
  219.     NumToString(line, lineString);
  220.     NumToString(MaxBlock(), memoryString);
  221.     
  222.     ParamText(fileString, dateString, lineString, memoryString);
  223.  
  224.     SetCursor(&qd.arrow);
  225.     switch (Alert(32767, 0)) {
  226.         case 1:
  227.             Debugger();
  228.             break;
  229.         case 2:
  230.             ExitToShell();
  231.             break;
  232.         case 3:
  233.             ExecutionBody();
  234.             break;
  235.         case 4:
  236.             break;
  237.     }
  238. }
  239.  
  240. #endif
  241.